import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.xbj.cloud.commons.annotation.XbjTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: 观雾
 * @create: 2024-09-08
 * @description:使用自定义单测注解
 **/
public class AutoTestClient {
    private static final Logger LOG = LoggerFactory.getLogger(AutoTestClient.class);

    public static void main(String[] args) {
        CalcHelpDemo calcHelpDemo = new CalcHelpDemo();
        int x = 10;
        int y = 0;
        Method[] methods = calcHelpDemo.getClass().getMethods();
        AtomicInteger bugCount = new AtomicInteger();  //bu总数
        String filePath = "BUgReport"+ DateUtil.format(new Date(),"yyyyMMddHHmmss")+".txt";
        for (Method method :methods ) {
            if(method.isAnnotationPresent(XbjTest.class)){
                try {
                    method.invoke(calcHelpDemo,x,y);
                } catch (Exception e) {
                    bugCount.getAndIncrement() ;
                    LOG.info("异常名称:"+e.getClass().getSimpleName()+"异常原因:"+e.getCause().getMessage());
                    FileUtil.writeString(method.getName()+"\t"+"出现了异常"+"\n",filePath,"UTF-8");
                    FileUtil.appendUtf8String("异常名称:"+e.getClass().getSimpleName()+"\n",filePath);
                    FileUtil.appendUtf8String("异常原因:"+e.getCause().getMessage()+"\n",filePath);
                }finally {
                    FileUtil.appendUtf8String("异常数:"+bugCount.get()+"\n",filePath);
                }

            }
        }


    }
}
